//
//  RHNewFeatureView.swift
//  微博新浪
//
//  Created by hezerong on 16/6/10.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

class RHNewFeatureView: UIView {
    
    //新特性页，滑动页
    lazy var scrollView:UIScrollView = {
    
       let scrollView = UIScrollView()
        
        //分页效果
        scrollView.pagingEnabled = true
        //取消弹簧效果
        scrollView.bounces = false
        //设置代理
        scrollView.delegate = self
        
        scrollView.contentSize = CGSizeMake(screenWidth * 5, 0)
        

        
        return scrollView
    }()
    
    //pageControl
    lazy var pageControl:UIPageControl = {
    
        let pageControl = UIPageControl()
        
        pageControl.numberOfPages = 4
        pageControl.currentPage = 0
        pageControl.pageIndicatorTintColor = UIColor.whiteColor()
        pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()

        return pageControl
    
    }()

  
    init(){
    
        super.init(frame: screenBounds)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension RHNewFeatureView{

    func setUpUI(){
    
//          self.backgroundColor = UIColor.greenColor()
        //添加子控件
        addSubview(scrollView)
        addSubview(pageControl)

               //添加特性内容
        for i in 1...4{
        
            let imageView = UIImageView(imageName: "new_feature_\(i)")
            scrollView.addSubview(imageView)
            
             //布局
            imageView.snp_makeConstraints(closure: { (make) -> Void in
                
                let offset = CGFloat(i - 1) * screenWidth
                
                make.top.equalTo(scrollView)
                make.left.equalTo(scrollView).offset(offset )
                make.size.equalTo(screenBounds.size)
            })
        
        }
        

        //布局scrollview 和  pagecontrol
        scrollView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.size.equalTo(self)
            let lastImageView = scrollView.subviews.last
            make.right.equalTo(lastImageView!).offset(screenWidth)
            
            
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(200)
        }
        
        
    }
}

extension RHNewFeatureView:UIScrollViewDelegate{

    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let contentoffset = scrollView.contentOffset.x
        
        if contentoffset >= 4 * screenWidth {
        
           self.removeFromSuperview()
        }
        
        
        let page = Int (contentoffset/screenWidth + 0.5)  //等效于 (contentoffset + screenWidth * 0.5) / screenWidth
        
        pageControl.currentPage = page
        

        pageControl.hidden = contentoffset > 3 * screenWidth
        
    }

}